package divinity.basic
{
	import flash.events.*;
	
	import flash.utils.getTimer;
	
	import divinity.basic.DivinityObject;
	import divinity.events.WorldEvent;
	public class DivinityEngine extends DivinityObject
	{
		private var _god:*=this;
		private var _startTime:int;
		private var _elapsed:int;		
		private var _timeFactor:Number=1;
		public function DivinityEngine(mygod:*=null)
		{
		
			if(mygod!=null)
			{
				_god=mygod;
			}
			log="god is "+_god;
			super();
		}
		override public function init(e:Event)
		{
			super.init(e);
			log="Added to stage";
		}
		override public function destroy(e:Event)
		{
			super.destroy(e);
			log="Destroyed";
		}
		public function startWorld():void
		{
			log="********* START WORLD ******************";
			dispatchEvent(new WorldEvent(WorldEvent.LET_THERE_BE_LIGHT));
			addEventListener(Event.ENTER_FRAME,cycle);
			_startTime=getTimer();
		}
		public function stopWorld():void
		{
			log="********** STOP WORLD *********************";
			removeEventListener(Event.ENTER_FRAME,cycle);
			dispatchEvent(new WorldEvent(WorldEvent.CATACLYSAM));
		}
		protected function cycle(e:Event):void
		{
			_elapsed=getTimer();
			_elapsed-=_startTime;
			_elapsed*=_timeFactor;
			var _cycle:WorldEvent=new WorldEvent(WorldEvent.CYCLE)
			_cycle.elapsed=_elapsed;
			dispatchEvent(_cycle);
			
			_startTime=getTimer();
		}
		/***
		 * 
		 * 
		 * 
		 * 
		 * 
		 * */
		public function get GOD():*
		{
			return _god;
		}
		public function set GOD(g:*):void
		{
			_god=g;
		}
		public function get elapsed():int
		{
			return _elapsed;	
		}
		
		public function set timeFactor(tf:Number):void
		{
			trace("setting time factor "+tf);
			_timeFactor=tf;
		}
		public function get timeFactor():Number
		{
			return _timeFactor;
		}
	}
}